<template>
  <div>
    <div>
      <el-input style="width: 200px; " placeholder="请输入标题" v-model="newsname"/>
      <el-button type="primary" @click="getTableData(1)" style="margin-left: 10px">搜索</el-button>
      <el-button type="primary" @click="resetData">重置</el-button>
    </div>
    <div style="margin: 5px 0px">
      <el-button type="primary" plain @click="addNews">新增</el-button>
      <el-button type="danger" plain @click="delBatch">删除</el-button>
    </div>
    <el-table :data="tableData" stripe :header-cell-style="{background:'#df6e2c',color:'white'}"  @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"></el-table-column>
      <el-table-column prop="id" label="序号" align="center"></el-table-column>
      <el-table-column prop="title" label="标题" align="center"></el-table-column>
      <el-table-column prop="description" label="简介" align="center"></el-table-column>
      <el-table-column prop="content" label="内容" align="center">
        <template v-slot="scope">
          <el-button @click="showContent(scope.row)">显示内容</el-button>

        </template>
      </el-table-column>
      <el-table-column prop="author" label="作者" align="center"></el-table-column>
      <el-table-column prop="time" label="更新日期" align="center"></el-table-column>
      <el-table-column label="操作" align="center" width="180">
        <template v-slot="scope">
          <el-button type="primary" size="mini" plain @click="editNews(scope.row)">编辑</el-button>
          <el-button type="danger" size="mini" plain @click="del(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="block">
      <el-pagination
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-size="pageSize"
          layout="total, prev, pager, next"
          :total="total">
      </el-pagination>
    </div>

    <el-dialog title="新闻信息" :visible="formVisible" width="60%" @close="closeDialog" >
      <el-form :model="form" label-width="80px" style="padding-right: 20px" :rules="rules" ref="VisibleRef">
        <el-form-item label="标题" prop="title" >
          <el-input v-model="form.title"  placeholder="标题"></el-input>
        </el-form-item>
        <el-form-item label="简介" prop="description" >
          <el-input v-model="form.description"  placeholder="简介"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content" >
          <div id="div1"/>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="formVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="文本内容" :visible="formVisible1" width="60%" >
        <div class="w-e-text">
          <div v-html="content"/>
        </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="formVisible1 = false">确 认</el-button>
      </div>
    </el-dialog>>
  </div>
</template>

<script>
import E from 'wangeditor'
import hljs from 'highlight.js'
export default {
  name: "News",
  data(){
    return {
      ids:[],
      tableData:[],
      pageNum:1,  //当前页码数
      pageSize:5, //每页显示多少条数据
      total:0,
      title:'',
      formVisible:false,
      form:{},
      user:JSON.parse(localStorage.getItem('user') || '{}'),
      rules: {
        title: [{ required: true, message: '标题不能为空', trigger: 'blur' }],
      },
      editor: null,
      newsname:"",
      formVisible1:false,
      content:''
    }
  },
  methods:{
    showContent(item){
      this.formVisible1=true
      this.content=item.content
    },
    getTableData(pageNum){
      if (pageNum) this.pageNum=pageNum
      this.$request.get("/news/selectByPage",{
       params:{
         pageNum:this.pageNum,
         pageSize:this.pageSize,
         title:this.title
       }
      }).then(res=>{　　　　
        this.tableData=res.data.records
        this.total=res.data.total
      })
    },
    handleCurrentChange(pageNum){
      this.getTableData(pageNum)
    },
    resetData(){
      this.name=''
      this.newsname=''
      this.getTableData(1)
    },

    save(){
      //获取编辑器的值
      this.form.content=this.editor.txt.html()
      this.$refs['VisibleRef'].validate(valid=>{
        if (valid){
          this.$request({
            url:'/news/add',
            method:"post",
            data:this.form
          }).then(res=>{
            if (res.code==='200'){
              this.$message.success('保存成功')
              this.getTableData()
              this.formVisible =false
            }else {
              this.$message.error(res.msg)
            }
          })
        }
      })
    },
    setRichText(){
      this.$nextTick(()=>{
        this.editor = new E(`#div1`)
        this.editor.config.uploadImgServer=this.$baseUrl+"/file/editor/upload"
        this.editor.config.uploadFileName="file"
        this.editor.config.uploadImgParams= {
          type: 'img',
        }
        this.editor.config.uploadImgHeaders={
          token:this.user.token
        }
        this.editor.config.uploadVideoServer=this.$baseUrl+"/file/editor/upload"
        this.editor.config.uploadVideoName="file"
        this.editor.config.uploadVideoHeaders={
          token:this.user.token
        }
        this.editor.config.uploadVideoParams = {
          type: 'video',
        }
        this.editor.create()
        this.editor.highlight=hljs
      })
    },
    addNews(){
      this.form={}
      this.formVisible=true
      // 创建编辑器
     this.setRichText()
    },
    editNews(news){
      this.form= {...news}
      this.formVisible=true
      this.setRichText()
      //给编辑器设置值
      setTimeout(()=>{
        this.editor.txt.html(this.form.content)
      },0)

    },

    closeDialog(){
      this.formVisible=false
      // 销毁编辑器
      this.editor.destroy()
      this.editor = null

    },
    del(id) {
      this.$confirm("确定删除吗？","确定删除",{type:"warning"}).then(resp=>{
        this.$request.post('/news/delete/'+id).then(res=>{
          if (res.code==='200'){
            this.$message.success("删除成功")
            this.getTableData(1)
          }else {
            this.$message.error(res.msg)
          }
        })
      }).catch(error => {})
    },
    handleSelectionChange(v){
      this.ids= v.map(res=>res.id)
    },
    delBatch(){
      if (this.ids.length<=0){
        this.$message.info("请选择您要删除的数据")
        return
      }
      this.$confirm("确定批量删除吗?","确认删除",{type:"warning"}).then(resp=>{
        this.$request.post("/news/delete/batch",this.ids).then(res=>{
          if (res.code==='200'){
            this.$message.success("删除数据成功")
            this.getTableData(1)
          }else {
            this.$message.error("删除数据失败")
          }
        })
      }).catch(err=>{})
    }
  },
  mounted() {
    this.getTableData()
  }
}
</script>

<style scoped>

</style>